﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ATW.MT.BUS;
using System.Text;
using ATW.MT.CommonClass;

namespace ATW.MT.SellManage
{
    public partial class SupplyManage : CommonPage
    {
        bus_T_Supply TS = new bus_T_Supply();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("../Main/Login.aspx");
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {//绑定
            string name = txtName.Value.Trim();
            string status = ddlStatus.SelectedValue;
            try
            {
                DataTable dt = TS.SupplyPagerQuery(name, status, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["TotalPagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnAddSupply_Click(object sender, EventArgs e)
        {//新增供货商邀请码
            string code = TS.getSupplyCode();
            ShowMsg("供应商邀请码为： <b>" + code + "</b>");
        }
        protected void lbnRange_Click(object sender, EventArgs e)
        {//供货范围
            LinkButton lbn = (LinkButton)sender;
            string uid = lbn.CommandArgument;
            Label lbnName = (Label)lbn.Parent.FindControl("lblName");
            string url = "../SellManage/SupplyRange.aspx?uid=" + uid + "&n=" + lbnName.Text + "&r=" + new Random().Next(1000).ToString();
            OpenDialog("供货范围管理", url, "height=600,width=800");
        }
        protected void lbnEdit_Click(object sender, EventArgs e)
        {//修改
            LinkButton lbn = (LinkButton)sender;
            string tauloginID = lbn.CommandArgument;
            string url = "../SellManage/_editSupply.aspx?uid=" + tauloginID + "&r=" + new Random().Next(1000).ToString();
            OpenDialog("修改供货商", url, "height=300,width=350");
        }
        protected void lbnCodeManage_Click(object sender, EventArgs e)
        {//邀请码管理
            string url = "../SellManage/_SupplyCodeManage.aspx?r=" + new Random().Next(1000);
            OpenDialog("邀请码管理", url, "height=400,width=800");
        }
        protected void lbnUserSupply_Click(object sender, EventArgs e)
        {//用户历史报价
            LinkButton lbn = (LinkButton)sender;
            string uid = lbn.CommandArgument;
            string url = "../SellManage/SupplyResponse.aspx?uid=" + uid + "&r=" + new Random().Next();
            OpenDialog("历史报价", url, "height=600,width=800");
        }
        protected void lbnDel_Click(object sender, EventArgs e)
        {//删除
            LinkButton lbn = (LinkButton)sender;
            string tauLoginID = lbn.CommandArgument;
            try
            {
                int result = TS.SupplyDel(tauLoginID);
                if (result > 0)
                {
                    Tips("删除成功！");
                    BindData();
                }
                else
                {
                    Tips("删除失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnRecover_Click(object sender, EventArgs e)
        {//恢复
            LinkButton lbn = (LinkButton)sender;
            string tauLoginID = lbn.CommandArgument;
            try
            {
                int result = TS.SupplyRecover(tauLoginID);
                if (result > 0)
                {
                    Tips("恢复成功！");
                    BindData();
                }
                else
                {
                    Tips("恢复失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }
        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
        private void ShowMsg(string msg)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                var close=document.getElementById('hideMsg');
                document.getElementById('msgContent').innerHTML='" + msg + @"';
                divTips.Show();
                close.onclick=function(){divTips.Close()};
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "Msg", strScript.ToString());
        }
    }
}